#include "stdafx.h"
#include "Quad.h"

using namespace MomogenkyouSystem;

Vertex	Quad::g_constVertices[4] =
{
	Vertex(0, 0, 0, 0, 1),
	Vertex(0, 0, 0, 1, 1), 
	Vertex(0, 0, 0, 1, 0), 
	Vertex(0, 0, 0, 0, 0), 
};

Vertex	Quad::g_Vertices[4] =
{
	Vertex(0, 0, 0, 0, 1),
	Vertex(0, 0, 0, 1, 1), 
	Vertex(0, 0, 0, 1, 0), 
	Vertex(0, 0, 0, 0, 0), 
};

const	short	Quad::g_Indices[6] = {0, 1, 2, 2, 3, 0};

void	Quad::RenderQuad(
const	float2	&	vWidthHeight, 
const	float4	&	vPosition, 
const	float4	&	vColor,
const	float	&	fRotation,
		bool		bXZ)
{
	TransformQuadVertex(g_constVertices, vWidthHeight, vPosition, vColor, fRotation, bXZ);

	GraphicsSystem::Device->SetFVF(Vertex::FVF);
	GraphicsSystem::Device->SetVertexDeclaration(Vertex::Declaration());
	GraphicsSystem::Device->DrawIndexedPrimitiveUP(	D3DPT_TRIANGLELIST, 0, 4, 2, g_Indices, 
												D3DFMT_INDEX16, g_constVertices, sizeof(Vertex));	
}

void	Quad::RenderQuad(
const	float2	&	vTxPosition,
const	float2	&	vTxSize, 
const	float2	&	vWidthHeight, 
const	float4	&	vPosition, 
const	float4	&	vColor,
const	float	&	fRotation,
		bool		bXZ)
{
	g_Vertices[0].TexCoord =  vTxPosition;
	g_Vertices[1].TexCoord =  vTxPosition;
	g_Vertices[2].TexCoord =  vTxPosition;
	g_Vertices[3].TexCoord =  vTxPosition;

	g_Vertices[0].TexCoord.x += vTxSize.x;
	g_Vertices[1].TexCoord.x +=	vTxSize.x;
	g_Vertices[1].TexCoord.y += vTxSize.y;
	g_Vertices[2].TexCoord.x += vTxSize.x;

	TransformQuadVertex(g_Vertices, vWidthHeight, vPosition, vColor, fRotation, bXZ);

	GraphicsSystem::Device->SetFVF(Vertex::FVF);
	GraphicsSystem::Device->SetVertexDeclaration(Vertex::Declaration());
	GraphicsSystem::Device->DrawIndexedPrimitiveUP(	D3DPT_TRIANGLELIST, 0, 4, 2, g_Indices, 
													D3DFMT_INDEX16, g_Vertices, sizeof(Vertex));	
}


void	Quad::RenderQuad(const	Vertex	*	pVertices)
{
	GraphicsSystem::Device->SetFVF(Vertex::FVF);
	GraphicsSystem::Device->SetVertexDeclaration(Vertex::Declaration());
	GraphicsSystem::Device->DrawIndexedPrimitiveUP(	D3DPT_TRIANGLELIST, 0, 4, 2, g_Indices, 
													D3DFMT_INDEX16, pVertices, sizeof(Vertex));	
}

void	Quad::TransformQuadVertex(
		Vertex	*	pVertices,
 const	float2	&	vWidthHeight, 
 const	float4	&	vPosition, 
 const	float4	&	vColor,
 const	float	&	fRotation,
		bool		bXZ)
{
	float	_Cos;
	float	_Sin;

	float	_W = vWidthHeight.x * 0.5f;
	float	_H = vWidthHeight.y * 0.5f;

	float	* _b0, *_b1, *_b2, * _b3;

	if(bXZ)
	{
		_b0 = &pVertices[0].Position.z;
		_b1 = &pVertices[1].Position.z;
		_b2 = &pVertices[2].Position.z;
		_b3 = &pVertices[3].Position.z;
	}
	else
	{
		_b0 = &pVertices[0].Position.y;
		_b1 = &pVertices[1].Position.y;
		_b2 = &pVertices[2].Position.y;
		_b3 = &pVertices[3].Position.y;
	}

	pVertices[0].Position = vPosition;
	pVertices[1].Position = vPosition;
	pVertices[2].Position = vPosition;
	pVertices[3].Position = vPosition;

	pVertices[0].Color = vColor;
	pVertices[1].Color = vColor;
	pVertices[2].Color = vColor;
	pVertices[3].Color = vColor;

	if (fRotation != 0.0f)
	{
		_Cos = cosf(fRotation);
		_Sin = sinf(fRotation);

		pVertices[0].Position.x  += -_W * _Cos - -_H * _Sin;
		*_b0  += -_W * _Sin + -_H * _Cos;	

		pVertices[1].Position.x  += _W * _Cos - -_H * _Sin;
		*_b1  += _W * _Sin + -_H * _Cos;	

		pVertices[2].Position.x  += _W * _Cos - _H * _Sin;
		*_b2  += _W * _Sin + _H * _Cos;	

		pVertices[3].Position.x  += -_W * _Cos - _H * _Sin;
		*_b3  += -_W * _Sin + _H * _Cos;	
	}
	else
	{
		pVertices[0].Position.x += -_W; 
		*_b0 += -_H;

		pVertices[1].Position.x += _W; 
		*_b1 += -_H;

		pVertices[2].Position.x += _W; 
		*_b2 += _H;

		pVertices[3].Position.x += -_W;
		*_b3 += _H;
	}	
}